package com.wonder.app.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wonder.app.domain.*;
import com.wonder.app.mapper.IpadUserDao;
import com.wonder.app.request.IpadUserRequest;
import com.wonder.app.request.OtherInRequest;
import com.wonder.app.service.*;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.DateUtils;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.file.FileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 *
 * <p>说明： 对接工业平板接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    Dec 21, 2023
 *
 */
@Api(tags = "工业平板")
@RestController
@RequestMapping("/app/if")
public class XqcifController {


    @Autowired
    private IpadUserService ipadUserService;

    @Autowired
    private PickMaterialService pickMaterialService;

    @Autowired
    private ProorderService proorderService;

    @Autowired
    private TransferService transferService;

    @Autowired
    private SupplimentService supplimentService;

    @Autowired
    private ReturnMaterialService returnMaterialService;

    @Autowired
    private InStockService inStockService;

    @Autowired
    private OtherInService otherInService;

    @Autowired
    private OtherOutService otherOutService;

    @Autowired
    private IpadUserDao ipadUserDao;

    @Autowired
    private PurchaseOrderService purchaseOrderService;

    @Autowired
    private ProcureService procureService;

    @Autowired
    private InventoryService inventoryService;



    @ApiOperation(value = "用户信息")
    @RequestMapping(value = "/user_list", method = RequestMethod.POST)
    public Map<String,Object> userList(@RequestBody JSONObject map)throws Exception {
        Map<String,Object> retMap = ipadUserService.getUserList(map);

        return AjaxMap.success(retMap);
    }

    @ApiOperation(value = "修改用户信息")
    @ResponseBody
    @PostMapping(value = "/update_user")
    public AjaxResult<Map<String,Object>> updateUser(@RequestBody JSONObject map) {
       if (null == map){
           return AjaxResult.failed("system.parameterError");
       }
       Map<String,Object> data = ipadUserService.updateUser(map);
       if (null != data){
           return AjaxResult.success(data);
       } else {
           return AjaxResult.failed();
       }
    }

    /**
     * 根据任务单号获取领料信息
     * @param map
     * @return
     */
    @ApiOperation(value = "获取领料信息")
    @ResponseBody
    @PostMapping(value = "/get_pick_material_by_task")
    public AjaxResult<List<Map<String,Object>>> getPickMaterialByTask(@RequestBody JSONObject map) {
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        List<Map<String,Object>> data = proorderService.getPickMaterialByTask(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }

    /**
     * 生成领料的调拨单
     * @param map
     * @return
     */
    @ApiOperation(value = "生成领料的调拨单")
    @ResponseBody
    @PostMapping(value = "/save_pick_transfer")
    public AjaxResult<Boolean> savePickTransfer(@RequestBody JSONObject map) throws Exception{
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Boolean res = transferService.savePickMaterial(map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }
    /**
     * 生成补料单
     * @param map
     * @return
     */
    @ApiOperation(value = "生成补料单")
    @ResponseBody
    @PostMapping(value = "/save_suppliment_pad")
    public AjaxResult<Boolean> saveSuppliment(@RequestBody JSONObject map) throws Exception{
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Boolean res = supplimentService.saveSupplimentPad(map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }

    /**
     * 生成退料单
     * @param map
     * @return
     */
    @ApiOperation(value = "生成退料单")
    @ResponseBody
    @PostMapping(value = "/save_return_material_pad")
    public AjaxResult<Boolean> saveReturnMaterialPad(@RequestBody JSONObject map) throws Exception{
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Boolean res = returnMaterialService.saveReturnMaterialPad(map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }

    /**
     * 生成入库单
     * @param map
     * @return
     */
    @ApiOperation(value = "生成入库单")
    @ResponseBody
    @PostMapping(value = "/save_in_stock_pad")
    public AjaxResult<Boolean> saveInStockPad(@RequestBody JSONObject map) throws Exception{
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Boolean res = inStockService.saveInStockPad(map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }

    /**
     * 生成其他入库单
     * @param map
     * @return
     */
    @ApiOperation(value = "生成其他入库单")
    @ResponseBody
    @PostMapping(value = "/save_other_in_pad")
    public AjaxResult<Boolean> saveOtherInPad(@RequestBody JSONObject map) throws Exception{
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Boolean res = otherInService.saveOtherInPad(map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }

    /**
     * 生成其他出库单
     * @param map
     * @return
     */
    @ApiOperation(value = "生成其他出库单")
    @ResponseBody
    @PostMapping(value = "/save_other_out_pad")
    public AjaxResult<Boolean> saveOtherOutPad(@RequestBody JSONObject map) throws Exception{
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Boolean res = otherOutService.saveOtherOutPad(map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }

    /**
     * 生成其他出库单
     * @param map
     * @return
     */
    @ApiOperation(value = "获取当天的调拨单信息")
    @ResponseBody
    @PostMapping(value = "/get_today_transfer")
    public AjaxResult<Map<String,Object>> getToadyTransfer(@RequestBody JSONObject map) throws Exception{
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = transferService.getToadyTransfer(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }


    /**
     * 订单开工保存员工工作信息
     * @param map
     * @return
     */
    @ApiOperation(value = "订单开工")
    @ResponseBody
    @PostMapping(value = "/prd_task_start")
    public AjaxResult<Boolean> prdTaskStart(@RequestBody JSONObject map) throws Exception{
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Boolean res = proorderService.prdTaskStart(map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }


    /**
     * 订单暂停保存员工工作信息
     * @param map
     * @return
     */
    @ApiOperation(value = "订单暂停")
    @ResponseBody
    @PostMapping(value = "/prd_task_stop")
    public AjaxResult<Boolean> prdTaskStop(@RequestBody JSONObject map) throws Exception{
        if (null == map || map.size() == 0){
            return AjaxResult.failed("参数不能为空");
        }
        Boolean res = proorderService.prdTaskStop(map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }

    /**
     * 订单结束保存员工工作信息
     * @param map
     * @return
     */
    @ApiOperation(value = "订单结束")
    @ResponseBody
    @PostMapping(value = "/prd_task_end")
    public AjaxResult<Boolean> prdTaskEnd(@RequestBody JSONObject map) throws Exception{
        if (null == map || map.size()==0){
            return AjaxResult.failed("system.parameterError");
        }
        Boolean res = proorderService.prdTaskEnd(map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }

    /**
     * 生成领料单
     * @param map
     * @return
     */
    @ApiOperation(value = "生成领料单")
    @ResponseBody
    @PostMapping(value = "/save_pick_material_pad")
    public AjaxResult<Boolean> savePickMaterialPad(@RequestBody JSONObject map) throws Exception{
        if (null == map || map.size()==0){
            return AjaxResult.failed("system.parameterError");
        }
        Boolean res = pickMaterialService.savePickMaterialPad(map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }


    @ApiOperation(value = "获取生产订单信息(包含详情表)")
    @RequestMapping(value = "/get_pro_order_list", method = RequestMethod.POST)
    public Map<String,Object> getProOrderList(@RequestBody JSONObject map)throws Exception {
        if (null == map || map.size() == 0){
            throw new Exception("参数不能为空");
        }
        int pageIndex = map.getInt("pageIndex");
        int pageSize = map.getInt("pageSize");
        String startTime = map.getStr("startTime");
        //如果开始时间未空的话默认查询当前当天的数据
        if (StringUtils.isEmpty(startTime)){
            startTime = DateUtils.dateToStr(new Date(),"yyyy-MM-dd" +  " 00:00:00");
        }
        String endTime = map.getStr("endTime");
        if (StringUtils.isEmpty(endTime)){
            endTime = DateUtils.dateToStr(new Date(),"yyyy-MM-dd" +  " 23:59:59");
        }
        String billNo = map.getStr("billNo");

        Map<String,Object> data =  proorderService.getProOrderList(pageIndex,pageSize,startTime,endTime,billNo);

        return AjaxMap.success(data);
    }

    @ApiOperation(value = "根据订单号获取订单下开工人员信息")
    @RequestMapping(value = "/get_user_by_billNo", method = RequestMethod.POST)
    public Map<String,Object> getUserByBillNo(@RequestBody JSONObject map)throws Exception {
        String billNo = map.getStr("billNo");
        Map<String,Object> data = proorderService.getUserByBillNo(billNo);

        return AjaxMap.success(data);
    }

    @ApiOperation(value = "获取采购订单(包含详情表)")
    @RequestMapping(value = "/get_pur_order_list", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> getPurOrderList(@RequestBody JSONObject map)throws Exception {
        if (null == map || map.size() == 0){
            throw new Exception("参数不能为空");
        }
        int pageIndex = map.getInt("pageIndex");
        int pageSize = map.getInt("pageSize");
        String billNo = map.getStr("billNo");

        Map<String,Object> data =  purchaseOrderService.getPurOrderList(pageIndex,pageSize,billNo);

        return AjaxResult.success(data);
    }

    @ApiOperation(value = "保存采购入库单(工业平板)")
    @RequestMapping(value = "/save_procure_pad", method = RequestMethod.POST)
    public AjaxResult<?> saveProcurePad(@RequestBody JSONObject map)throws Exception {
        if (null == map || map.size() == 0){
            throw new Exception("参数不能为空");
        }
        boolean data =  procureService.saveProcurePad(map);
        if (data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }


    }

    @ApiOperation(value = "读取excle导入及时库存")
    @RequestMapping(value = "/read_inventory", method = RequestMethod.POST)
    public AjaxResult<?> readInventory(@RequestBody JSONObject map)throws Exception {

        boolean data =  inventoryService.readInventory(map);

        return AjaxResult.success(data);
    }


    /**
     * 订单暂停保存员工工作信息
     * @param map
     * @return
     */
    @ApiOperation(value = "订单暂停(全部暂停)")
    @ResponseBody
    @PostMapping(value = "/prd_task_stop_all")
    public AjaxResult<Boolean> prdTaskStopAll(@RequestBody JSONObject map) throws Exception{
        if (null == map || map.size() == 0){
            return AjaxResult.failed("参数不能为空");
        }
        Boolean res = proorderService.prdTaskStopAll(map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }


    /**
     * 订单结束保存员工工作信息
     * @param map
     * @return
     */
    @ApiOperation(value = "订单结束(全部结束)")
    @ResponseBody
    @PostMapping(value = "/prd_task_end_all")
    public AjaxResult<Boolean> prdTaskEndAll(@RequestBody JSONObject map) throws Exception{
        if (null == map || map.size()==0){
            return AjaxResult.failed("system.parameterError");
        }
        Boolean res = proorderService.prdTaskEndAll(map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }

    /**
     * 根据时间和车间获取调拨数量,以及对应岗上仓库剩余的数量
     * @param map
     * @return
     */
    @ApiOperation(value = "根据时间和车间获取调拨数量,以及对应岗上仓库剩余的数量")
    @ResponseBody
    @PostMapping(value = "/get_transfer_and_workshop")
    public AjaxResult<Map<String,Object>> getTransferAndWorkshop(@RequestBody JSONObject map) throws Exception{
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = transferService.getTransferAndWorkshop(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }

}
